package com.qsw.refreshrecyclerview.RefreshRecyclerView.view;

import android.graphics.Rect;
import android.support.v7.widget.RecyclerView;
import android.view.View;

/**
 * RecyclerView表格布局边距
 */
public class GridSpacingItemDecoration extends RecyclerView.ItemDecoration {

    private int mJumpCount;// 跳过数量（不设置边距）
    private int mItemType; // 要设置边距的条目类型
    private int mSpanCount; // 列数
    private int mHSpacing; // 水平间隔
    private int mVSpacing; // 垂直隔

    public GridSpacingItemDecoration(int spanCount, int hSpacing, int vSpacing) {
        this(0, 0, spanCount, hSpacing, vSpacing);
    }

    public GridSpacingItemDecoration(int jumpCount, int itemType, int spanCount, int hSpacing, int vSpacing) {
        this.mJumpCount = jumpCount;
        this.mItemType = itemType;
        this.mSpanCount = spanCount;
        this.mHSpacing = hSpacing;
        this.mVSpacing = vSpacing;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        int position = parent.getChildAdapterPosition(view) - mJumpCount;
        if (position >= 0 && parent.getChildViewHolder(view).getItemViewType() == mItemType) {
            int column = position % mSpanCount;
            outRect.left = column * mHSpacing / mSpanCount;
            outRect.right = mHSpacing - (column + 1) * mHSpacing / mSpanCount;
            outRect.top = mVSpacing / 2;
            outRect.bottom = mVSpacing / 2;
        }
    }
}
